package com.glodon.ggzy.core.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

/**
 * 封装SpringJdbc类.
 * 
 * 可在Service层直接使用,也可以扩展泛型DAO子类使用.
 * @author yangjf 
 */

@SuppressWarnings("deprecation")
public class SimpleSpringJdbcDao extends SimpleJdbcDaoSupport implements IJdbcDao {

	@Override
	public int queryForInt(String sql) {
		return this.getJdbcTemplate().queryForInt(sql);
	}

	@Override
	@SuppressWarnings("rawtypes")
	public String queryForString(String sql) {
		String result = null;
		List sqlData = this.queryList(sql);
		Iterator sqlIt = sqlData.iterator();
		while (sqlIt.hasNext()) {
			result = ((Map) (sqlIt.next())).values().iterator().next().toString();
			break;
		}
		return result;
	}

	@Override
	@SuppressWarnings("rawtypes")
	public List queryList(String sql) {
		return this.getJdbcTemplate().queryForList(sql);
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<String> queryColunmList(String sql) {
		List<String> list = this.getJdbcTemplate().query(sql, new RowMapper() {

			@Override
			public Object mapRow(java.sql.ResultSet rs, int index) throws SQLException {
				return rs.getString("regionname");
			}
		});
		return list;
	}

	@Override
	public void excuteSql(String sql) {
		this.getJdbcTemplate().execute(sql);
	}

	@Override
	public void excuteBatchUpdate(String[] sqlAry) {
		this.getJdbcTemplate().batchUpdate(sqlAry);
	}

	@Override
	public int queryOracleSeqNextValue(String sequence) {
		return queryForInt("select " + sequence + ".nextval from dual");
	}

}
